package com.seele.tigerwallet.ui.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.seele.tigerwallet.R;
import com.seele.tigerwallet.model.FoundModel;
import com.seele.tigerwallet.model.WebModel;
import com.seele.tigerwallet.ui.MainActivity;
import com.seele.tigerwallet.ui.activtiy.MessageActivity;
import com.seele.tigerwallet.ui.activtiy.SystemSetActivtiy;
import com.seele.tigerwallet.ui.activtiy.WebViewActivtiy;
import com.seele.tigerwallet.ui.adapter.FoundApplicationAdapter;
import com.seele.tigerwallet.ui.adapter.FoundSeviceAdapter;
import com.seele.tigerwallet.ui.base.BaseFragment;
import com.seele.tigerwallet.ui.views.FoundHreadView;
import com.seele.tigerwallet.ui.views.LinearRelativeLayout;

import java.util.ArrayList;
import java.util.List;


/**
 * @Author: 郑波
 * @Time: 2018/6/7 19:56
 * @Description: 发现页面
 */
public class FoundFrgament extends BaseFragment implements View.OnClickListener {

    private TextView mTitle;

    private ImageView mIcon;
    private LinearLayout mLinearLayout;

    private static FoundFrgament foundFrgament;

    private RecyclerView mSeviceRecyclerView;

    private RecyclerView mApplicationRecyclerView;

    private FoundSeviceAdapter mFoundSeviceAdapter;

    private FoundApplicationAdapter mFoundApplicationAdapter;

    private List<FoundModel> mSeviceList;

    private List<FoundModel> mApplicationList;

    private List<WebModel> mWebModels;

    private int count = 3;


    public static FoundFrgament newInstanec() {
        if (foundFrgament == null) {
            foundFrgament = new FoundFrgament();
        }
        return foundFrgament;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_found;
    }

    @Override
    protected void initView() {
        mTitle = $f(R.id.title_tv);
        mTitle.setText(getString(R.string.main_found));
        mSeviceRecyclerView = $f(R.id.sevice_recyclerView);
        mIcon = $f(R.id.add_icon);
        mIcon.setImageDrawable(getResources().getDrawable(R.drawable.message));
        mLinearLayout = $f(R.id.add_ll);
        mLinearLayout.setOnClickListener(this);
        mIcon.setVisibility(View.VISIBLE);
        mSeviceRecyclerView.setLayoutManager(new CustomLinearLayoutManager(mContext, 3));
        mApplicationRecyclerView = $f(R.id.application_recyclerview);
        mApplicationRecyclerView.setLayoutManager(new CustomLinearLayoutManager(mContext, 3));
        initAdapter();
//        initSeviceData();

        getWebModels();
        initApplicationData();
    }

    private void initApplicationData() {
        if (null == mApplicationList) {
            mApplicationList = new ArrayList<>();
        }

        FoundModel coinmarketcap = new FoundModel();
        coinmarketcap.resId = R.drawable.icon_coinmarketcap;
        coinmarketcap.title = getResources().getString(R.string.coinmarketcap);
        mApplicationList.add(coinmarketcap);

        FoundModel ethBrowser = new FoundModel();
        ethBrowser.resId = R.drawable.ic_eth;
        ethBrowser.title = getResources().getString(R.string.eth_browser);
        mApplicationList.add(ethBrowser);

        FoundModel btcBrowser = new FoundModel();
        btcBrowser.resId = R.drawable.ic_btc;
        btcBrowser.title = getResources().getString(R.string.btc_browser);
        mApplicationList.add(btcBrowser);

        mFoundApplicationAdapter.setNewData(mApplicationList);
        mFoundApplicationAdapter.notifyDataSetChanged();
        mFoundApplicationAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Bundle bundle = new Bundle();
                bundle.putSerializable(WebViewActivtiy.class.getSimpleName(), mWebModels.get(position));
                startActivity(WebViewActivtiy.class, bundle);

            }
        });

    }

    /**
     * 创建数据
     */
    private void initSeviceData() {
        if (null == mSeviceList) {
            mSeviceList = new ArrayList<>();
        }
        FoundModel icon = new FoundModel();
        icon.resId = R.drawable.icon_ico;
        icon.title = getResources().getString(R.string.ico_project);
        mSeviceList.add(icon);
        FoundModel candy = new FoundModel();
        candy.resId = R.drawable.icon_candy;
        candy.title = getResources().getString(R.string.candy_project);
        mSeviceList.add(candy);
        FoundModel coins = new FoundModel();
        coins.resId = R.drawable.icon_currency;
        coins.title = getResources().getString(R.string.coins_project);
        mSeviceList.add(coins);
        mFoundSeviceAdapter.setNewData(mSeviceList);
        mFoundSeviceAdapter.notifyDataSetChanged();


    }

    private void initAdapter() {

        mFoundSeviceAdapter = new FoundSeviceAdapter(R.layout.item_found);

        mFoundApplicationAdapter = new FoundApplicationAdapter(R.layout.item_found);

        FoundHreadView foundHreadView = new FoundHreadView(mContext);
        foundHreadView.setTitle(getResources().getString(R.string.tiger_wallet_sevice));

        FoundHreadView applocationHreadView = new FoundHreadView(mContext);
        applocationHreadView.setTitle(getResources().getString(R.string.thride));

        mFoundApplicationAdapter.addHeaderView(applocationHreadView);
        mFoundSeviceAdapter.addHeaderView(foundHreadView);
        mSeviceRecyclerView.setAdapter(mFoundSeviceAdapter);
        mApplicationRecyclerView.setAdapter(mFoundApplicationAdapter);


    }

    public void getWebModels() {
        if (null == mWebModels) {
            mWebModels = new ArrayList<>();
        }
        WebModel coinmarketcap = new WebModel();
        coinmarketcap.title = getResources().getString(R.string.coinmarketcap);
        coinmarketcap.url = "https://coinmarketcap.com";

        mWebModels.add(coinmarketcap);
        WebModel etherscan = new WebModel();
        etherscan.title = getResources().getString(R.string.block_chain_browser);
        etherscan.url = "http://etherscan.io";

        mWebModels.add(etherscan);
        WebModel btc = new WebModel();
        btc.title = getResources().getString(R.string.btc_transaction);
        btc.url = "https://btc.com";
        mWebModels.add(btc);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.add_ll:
                //TODO 跳转消息界面
                startActivity(MessageActivity.class);
                break;
        }
    }


    public class CustomLinearLayoutManager extends GridLayoutManager {
        private boolean isScrollEnabled = true;

        public CustomLinearLayoutManager(Context context, int spanCount) {
            super(context, spanCount);
        }


        public void setScrollEnabled(boolean flag) {
            this.isScrollEnabled = flag;
        }

        @Override
        public boolean canScrollVertically() {
            //Similarly you can customize "canScrollHorizontally()" for managing horizontal scroll
            return isScrollEnabled && super.canScrollVertically();
        }
    }

//    @Override
//    public void onClick(View view) {
//        switch (view.getId()) {
//            case R.id.coinmarketcap_rv:
//                webModel = new WebModel();
//                webModel.title = getResources().getString(R.string.coinmarketcap);
//                webModel.url = "https://coinmarketcap.com";
//                bundle = new Bundle();
//                bundle.putSerializable(WebViewActivtiy.class.getSimpleName(), webModel);
//                startActivity(WebViewActivtiy.class, bundle);
//                break;
//            case R.id.browser_rv:
//                webModel = new WebModel();
//                webModel.title = getResources().getString(R.string.block_chain_browser);
//                webModel.url = "http://etherscan.io";
//                bundle = new Bundle();
//                bundle.putSerializable(WebViewActivtiy.class.getSimpleName(), webModel);
//                startActivity(WebViewActivtiy.class, bundle);
//                break;
//            case R.id.btc_rv:
//                webModel = new WebModel();
//                webModel.title = getResources().getString(R.string.btc_transaction);
//                webModel.url = "https://btc.com";
//                bundle = new Bundle();
//                bundle.putSerializable(WebViewActivtiy.class.getSimpleName(), webModel);
//                startActivity(WebViewActivtiy.class, bundle);
//                break;
//        }
//    }
}
